package com.linqi.freebie.utils


import java.io.File


/**
 *
 */
object SteamPathFinder {

    fun getSteamPath(): String? {
        return when {
            isWindows() -> getWindowsSteamPath()
            isMacOS() -> getMacOSSteamPath()
            else -> null
        }
    }

    private fun getWindowsSteamPath(): String? {
        return try {
            // 尝试通过PowerShell获取
           return getSteamPathViaPowerShell()
        } catch (e: Exception) {
            null
        }
    }

    private fun getMacOSSteamPath(): String? {
        val macPaths = listOf(
            "${System.getProperty("user.home")}/Library/Application Support/Steam",
            "/Applications/Steam.app/Contents/MacOS"
        )

        return macPaths.firstOrNull { File(it).exists() }
    }

    private fun getSteamPathViaPowerShell(): String? {
        return try {
            val process = Runtime.getRuntime().exec(
                arrayOf(
                    "powershell.exe",
                    "(Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Wow6432Node\\Valve\\Steam').InstallPath"
                )
            )
            process.inputStream.bufferedReader().use { it.readLine() }
        } catch (e: Exception) {
            null
        }
    }





    private fun isWindows(): Boolean {
        return System.getProperty("os.name").lowercase().contains("win")
    }

    private fun isMacOS(): Boolean {
        return System.getProperty("os.name").lowercase().run {
            contains("mac") || contains("os x") || contains("darwin")
        }
    }

    private fun isLinux(): Boolean {
        return System.getProperty("os.name").lowercase().run {
            contains("linux") || contains("unix") || contains("sunos") || contains("bsd")
        }
    }


}
